/**
 * package-info.java com.mycompany.project.client.
 */
package com.mycompany.project.client.neville;
import com.mycompany.project.client.chee.HabitatConfiguration;
import com.mycompany.project.client.chee.Module;
import com.mycompany.project.client.chee.ModuleList;
import com.mycompany.project.client.ModuleType;

/**
 * Quality Check GUI Class.
 * 
 * @author Nick Neville, Chee Lee
 * @version 1.0.
 * 
 **/
public class QualityCheck {
	
	/**
	 * int moduleCount.
	 */
	private static int moduleCount;
	
	/**getModuleCount().
	 * 
	 * @return moduleCount;
	 */
	public static int getModuleCount() {
		return moduleCount;
	}
	
	/** calculateConfigQuality() method.
	 * @param config the Habitat Configuration.
	 * Sanitation not next to Canteen.
	 * Sanitation not next to Food & Water Storage.
	 * Airlock not next to Dormitory.
	 * At least on part of the habitat viewable from another part.
	 */
	static final String calculateConfigQuality(final HabitatConfiguration config) {
		String temp;
		moduleCount = 0;
		StringBuffer diagnlCheckResult = new StringBuffer("");
		if (config.getGridList() == null || ModuleList.checkConfiguration() != 1) {
			temp = "<br><b>Nothing to check</b></br>";
		}

		Module[][] moduleMap = config.getmGrid();
		
		//staring at i = 1, Control Module, skip plains
		for (int i = 1; i < config.getGridList().size(); i++) {
				if (i != ModuleType.CONTROL.ordinal() 
						&& i != ModuleType.POWER.ordinal() 
						&& i != ModuleType.MEDICAL.ordinal() 
						&& i != ModuleType.GYM_RELAXATION.ordinal()) {
			for (int j = 0; j < config.getGridList().get(i).size(); j++) {
				//check Diagonals of current layout configuration moduleType i 
				diagnlCheckResult.append(checkDiagonals(i, j, config, moduleMap));
		    }
		}
	}
		temp = "<b>Quality Check Results</b>\n" + diagnlCheckResult.toString() + "<br></br>" + moduleCount + " <b><font color=\"red\">"
			+ "module(s) failed to satisfy</br></font>";
		return temp;
	}

	/** checkDiagonals() method.
	 * @return an agrigate string w/ the associated Layout Rules separated by 'n/'.   
	 * @param iCount holds the module type information.
	 * @param jCount holds the current module in the specific type list.
	 * @param config the map congifuration.
	 * @param moduleMap the Module Map with each modlue coordinate.
	 */
	private static String checkDiagonals(final int iCount, final int jCount, final HabitatConfiguration config, final Module[][] moduleMap) {
	
		StringBuffer buildString = new StringBuffer("");
		
		//if iCount == the SANITATION module
		if (iCount == ModuleType.SANITATION.ordinal()) {
			//capture its row and column position
			int column = config.getGridList().get(iCount).get(jCount).getGridX();
			int row = config.getGridList().get(iCount).get(jCount).getGridY();
			
			//check if the local diagonal modules aren't CANTEEN || FOOD_WATER_STORAGE modules
			if ((moduleMap[row - 1][column - 1] != null && (moduleMap[row - 1][column - 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row - 1][column - 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row - 1][column + 1] != null && (moduleMap[row - 1][column + 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row - 1][column + 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row + 1][column + 1] != null && (moduleMap[row + 1][column + 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row + 1][column + 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row + 1][column - 1] != null && (moduleMap[row + 1][column - 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row + 1][column - 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row][column - 1] != null && (moduleMap[row][column - 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row][column - 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row + 1][column] != null && (moduleMap[row + 1][column].getModuleType() == ModuleType.CANTEEN || moduleMap[row + 1][column].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row - 1][column] != null && (moduleMap[row - 1][column].getModuleType() == ModuleType.CANTEEN || moduleMap[row - 1][column].getModuleType() == ModuleType.FOOD_WATER_STORAGE))
					|| (moduleMap[row][column + 1] != null && (moduleMap[row][column + 1].getModuleType() == ModuleType.CANTEEN || moduleMap[row][column + 1].getModuleType() == ModuleType.FOOD_WATER_STORAGE))) {
				buildString.append("<b>SANITATION</b><br>Module at Row: ");
				buildString.append(row);
				buildString.append(" Column: ");
				buildString.append(column);
				buildString.append("</br><b><font color = \"red\">DOES NOT<br>satisfy the layout rules!</br></b></font>" + "\n");
				moduleCount++; //increment the number of modules that does not satisfy the layout rules
			} else {
				
				//here if local modules aren't conflicting..
				//so this SANITATION module satisfies the layout rules!		
				buildString.append("<b>SANITATION</b><br>Module at Row: ");
				buildString.append(row);
				buildString.append(" Column: ");
				buildString.append(column);
				buildString.append("</br><b><font color = \"green\">DOES<br>satisfy the layout rules!</br></b></font>" + "\n");				
			} //if iCount == the AIR_LOCK module
		} else if (iCount == ModuleType.AIR_LOCK.ordinal()) { 
			//capture its row and column position
			int column = config.getGridList().get(iCount).get(jCount).getGridX();
			int row = config.getGridList().get(iCount).get(jCount).getGridY();
			 //check if the local diagonal module is a DORMITORY module
			if ((moduleMap[row - 1][column - 1] != null && moduleMap[row - 1][column - 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row - 1][column + 1] != null && moduleMap[row - 1][column + 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row + 1][column + 1] != null && moduleMap[row + 1][column + 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row + 1][column - 1] != null && moduleMap[row + 1][column - 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row + 1][column] != null && moduleMap[row + 1][column].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row][column - 1] != null && moduleMap[row][column - 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row][column + 1] != null && moduleMap[row][column + 1].getModuleType() == ModuleType.DORMITORY)
					|| (moduleMap[row - 1][column] != null && moduleMap[row - 1][column].getModuleType() == ModuleType.DORMITORY)) {
				buildString.append("<b>AIRLOCK</b><br>Module at Row: ");
				buildString.append(row);
				buildString.append(" Column: ");
				buildString.append(column);
				buildString.append("</br><b><font color = \"red\">DOES NOT<br>satisfy the layout rules!</br></b></font>" + "\n");
				moduleCount++; //increment the number of modules that does not satisfy the layout rules
			} else {				
						//here if local modules aren't conflicting..
						//so this AIRLOCK module satisfies the layout rules!		
						buildString.append("<b>AIRLOCK</b><br>Module at Row: ");
						buildString.append(row);
						buildString.append(" Column: ");
						buildString.append(column);
						buildString.append("</br><b><font color = \"green\">DOES<br>satisfy the layout rules!</br></b></font>" + "\n");				
					}
		}
		return buildString.toString();	
	}
}
